/*
 * Copyright 2007 Jaisy Sprl.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package be.jaisy.ui.gwt.widget.sample.client.table;

import java.util.ArrayList;
import java.util.List;

import be.jaisy.ui.gwt.widget.client.viewer.IContentProvider;
import be.jaisy.ui.gwt.widget.client.viewer.Viewer;
import be.jaisy.ui.gwt.widget.client.viewer.table.ITableLabelProvider;
import be.jaisy.ui.gwt.widget.client.viewer.table.TableViewer;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author Stany Blanvalet <stany.blanvalet@jaisy.be>
 */
public class TableViewerSimpleSample implements EntryPoint{

	public void onModuleLoad(){

		TableViewer table = new TableViewer();
		table.addHeader(0, "name", true);
		table.addHeader(0, "description", false);
		table.addFilter(0);
		table.setContentProvider(new IContentProvider(){
				public Object[] getElements(Object inputElement){
					return ((List)inputElement).toArray();
				}
				public void inputChanged(Viewer viewer, Object oldInput, Object newInput){
					// nothing to do
				}
			});
		
		
		table.setLabelProvider(new ITableLabelProvider(){
			public Object[] getRow(Object element){
				if (element instanceof SimpleTo){
					SimpleTo simpleTo = (SimpleTo)element;					
		 			return new Object[] {simpleTo.getName(), simpleTo.getDescription()};
		 		}
		 		return null;
		 	}
		 	public String getCellStyle(Object element, int columnIndex){
		 		return null;
		 	}
		 	public String getRowStyle(Object element){
		 		if (element instanceof SimpleTo){
		 			SimpleTo simpleTo = (SimpleTo)element;	
		 			if (simpleTo.getName().indexOf("?") >= 0)
		 				return "-red";
		 		}
		 		return null;
		 	}});
		 
		 	List inputs = new ArrayList();
		 	inputs.add(new SimpleTo("test1","description1"));
		 	inputs.add(new SimpleTo("test2?","description2"));
		 
		 	table.setInput(inputs);


		RootPanel.get().add(table);
	}

	public class SimpleTo{
		private String name;

		private String description;

		public SimpleTo(String name, String description){
			this.name = name;
			this.description = description;
		}

		public String getName(){
			return this.name;
		}

		public String getDescription(){
			return this.description;
		}
	}

}
